﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Linq.Expressions;
using olierFrm.DataBase.FrameWork.Plugin;
using olierFrm.GeneralCache;

namespace olierFrm.DataBase.FrameWork.DataDictornary
{
    public class DataDictionary
    {
        public const string DataDictionaryCachePrefix = "DataDictionary=";
        private static List<WF_DataDictionary> _DataDictionary
        {
            get;
            set;
        }
        public static Dictionary<string, WF_DataDictionary> FindByFieldName(string fieldName)
        {
            DataDictionary.Load();
            if (DataDictionary._DataDictionary.Count == 0)
            {
                return null;
            }
            var dds =
                from d in DataDictionary._DataDictionary
                where d.FieldName == fieldName
                select d;
            if (dds.Count<WF_DataDictionary>() == 0)
            {
                return null;
            }
            Dictionary<string, WF_DataDictionary> i = new Dictionary<string, WF_DataDictionary>();
            foreach (WF_DataDictionary d2 in dds)
            {
                i.Add(d2.Context, d2);
            }
            return i;
        }
        public static WF_DataDictionary FindByFieldNameContext(string fieldName, string context)
        {
            Dictionary<string, WF_DataDictionary> dds = DataDictionary.FindByFieldName(fieldName);
            if (dds == null || !dds.ContainsKey(context))
            {
                return null;
            }
            return dds[context];
        }
        private static void Load()
        {
            DataDictionary._DataDictionary = new List<WF_DataDictionary>();
            if (!Cache.KeyExists("DataDictionary="))
            {
                FrameWorkEntity _DBContext = new FrameWorkEntity(ConnectionConfig.FrmEntityConnectionStr);

                DataDictionary._DataDictionary = _DBContext.WF_DataDictionary.ToList();
                Cache.Add("DataDictionary=", DataDictionary._DataDictionary);
                return;
            }
            else
            {
                DataDictionary._DataDictionary = (List<WF_DataDictionary>)Cache.Get("DataDictionary=");
            }
        }
        public static void BindToDataTableByCaption(DataTable dt)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                Dictionary<string, WF_DataDictionary> dd = DataDictionary.FindByFieldName(dt.Columns[i].ColumnName);
                if (dd != null)
                {
                    dt.Columns[i].Caption = ((dd.First<KeyValuePair<string, WF_DataDictionary>>().Value.DisplayLabel.Trim().Length != 0) ? dd.First<KeyValuePair<string, WF_DataDictionary>>().Value.DisplayLabel.Trim() : dt.Columns[i].ColumnName);
                }
            }
        }
        public static void BindCaptionToDataTable(DataTable dt, List<string> DicContext = null)
        {
            Dictionary<string, string> diDicContext = null;
            if (DicContext != null)
            {
                diDicContext = new Dictionary<string, string>();
                foreach (string d in DicContext)
                {
                    string[] ds = d.Split(new char[]
					{
						'='
					});
                    diDicContext.Add(ds[0], ds[1]);
                }
            }
            foreach (DataColumn dc in dt.Columns)
            {
                Dictionary<string, WF_DataDictionary> d2 = DataDictionary.FindByFieldName(dc.ColumnName);
                if (d2 == null)
                {
                    continue;
                }
                WF_DataDictionary dInfo = (diDicContext != null && diDicContext.ContainsKey(dc.ColumnName)) ? d2[diDicContext[dc.ColumnName]] : d2.First().Value;
                if (dInfo.DisplayLabel != null && dInfo.DisplayLabel.Trim().Length != 0)
                {
                    dc.Caption = dInfo.DisplayLabel;
                }
                if (dc.ColumnName.Length > 4 && dc.ColumnName.Substring(dc.ColumnName.Length - 4) == "Prop" && dInfo.CommonStrTypeID.Trim().Length != 0)
                {
                    List<WS_CommonItems> common = (
                        from c in Common.FindByCommonNO(dInfo.CommonStrTypeID)
                        orderby c.nOrder
                        select c).ToList<WS_CommonItems>();
                    dc.ExtendedProperties.Add("PropCommon", common);
                    continue;
                    //暂时忽略
                    List<string> Prop;
                    if (dt.ExtendedProperties.ContainsKey("PropCommon"))
                    {
                        Prop = (List<string>)dt.ExtendedProperties["PropCommon"];
                    }
                    else
                    {
                        Prop = new List<string>();
                        dt.ExtendedProperties.Add("PropCommon", Prop);
                    }
                    Prop.Add(dc.ColumnName);
                    double cellwidth = 0.0;
                    foreach (WS_CommonItems c2 in common)
                    {
                        cellwidth += (double)Encoding.Default.GetBytes(c2.sComDesc).Length * 7.9 + 12.0;
                    }
                    int intTempHeader = Encoding.Default.GetBytes(dc.ColumnName).Length * 9;
                    if (cellwidth > (double)intTempHeader)
                    {
                        dc.ExtendedProperties.Add("PropCommonWidth", cellwidth);
                    }
                    else
                    {
                        dc.ExtendedProperties.Add("PropCommonWidth", intTempHeader);
                    }
                }
            }
        }
        public static int Text_Length(string Text)
        {
            int len = 0;
            for (int i = 0; i < Text.Length; i++)
            {
                byte[] byte_len = Encoding.Default.GetBytes(Text.Substring(i, 1));
                if (byte_len.Length > 1)
                {
                    len += 2;
                }
                else
                {
                    len++;
                }
            }
            return len;
        }
    }
}
